package com.toyota.toyotaownerspoc.main;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.AbsListView.LayoutParams;
import android.widget.ImageView.ScaleType;

import java.io.InputStream;

import com.toyota.toyotaownerspoc.R;

public class ItemAdapter extends ArrayAdapter<String> {

	private final Context context;
	private final String[] Ids;
	private final int rowResourceId;

	public ItemAdapter(Context context, int textViewResourceId, String[] objects) {

		super(context, textViewResourceId, objects);

		this.context = context;
		this.Ids = objects;
		this.rowResourceId = textViewResourceId;

	}

	@SuppressWarnings("finally")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View rowView = null;
		// get input stream
		try {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			if (inflater != null) {
				rowView = inflater.inflate(rowResourceId, parent, false);
				if (rowView != null) {
					ImageView imageView = (ImageView) rowView
							.findViewById(R.id.main_activity_icons_image_view);

					if (imageView != null) {
						int id = Integer.parseInt(Ids[position]);
						String resourceId = ListViewItemModelLoader.GetbyId(id).resourceFile;

						// load image as Drawable
						int resource = context.getResources().getIdentifier(resourceId,"drawable",context.getPackageName());
						Drawable drawable = context.getResources().getDrawable(resource);
					
				
						// set image to ImageView
						imageView.setImageDrawable(drawable);
			
						AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,drawable.getIntrinsicHeight()/2);
						rowView.setLayoutParams(params);
						
					

					} else {
						Log.d("***Class ItemAdapter , imageView: ",
								String.valueOf(imageView));

					}
				} else {
					Log.d("***Class ItemAdapter , rowView: ",
							String.valueOf(rowView));
				}
			} else {
				Log.d("***Class ItemAdapter , inflater: ",
						String.valueOf(inflater));
			}
		} catch (Exception exc) {
			Log.d("***Class ItemAdapter , getView(int position, View convertView, ViewGroup parent): ",
					exc.getMessage());
		} finally {
		
			return rowView;
		}

	}

}